/*jslint sloppy: true */
/*global require, module */
var forEach = require('./util').forEach,
	extend = require('./util').extend,
	Result = require('./result');

/**
 * @class Report
 * @constructor
 * @param task {Object} The task snapshot.
 * @param reportRaw {Array} Result list of this task.
 * @param [flag] {String|undefined} Use to check result belong to task or not.
 */
function Report(task, reportRaw, flag) {
	/**
	 * @property $overview
	 * @type Object
	 */
	this.$overview = {
		// from result
		name: reportRaw.name, //TODO
		envNumber: reportRaw.tR.length,

		// from task
		caseNumber: task.schemas.caseList.length,
		config: task.schemas.config,

		// summary
		passedCase: 0,
		failureCase: 0,
		passedFrequency: 0,
		failureFrequency: 0,
		totalTimes: 0,
		testingMachineTime: 0
	};
	/**
	 * @property $task
	 * @type Object
	 */
	this.$task = task;
	/**
	 * @property $overview
	 * @type array
	 */
	this.$resultList = this.$initializeTaskList(reportRaw);
}
/**
 * @method $initializeTaskList
 * @private
 */
Report.prototype.$initializeTaskList = function (reportRaw) {
	var taskList = [], rO = this.$overview;

	forEach(reportRaw.tR, function (taskResultRaw) {
		var tR = new Result(taskResultRaw, this),
			tRO = tR.getOverview();

		taskList.push(tR);
		rO.testingMachineTime += tRO.testingMachineTime;
		rO.passedFrequency += tRO.passedFrequency;
		rO.failureFrequency += tRO.failureFrequency;
		rO.passedCase += tRO.allPassedNumber;
		rO.failureCase += tRO.hasFailureNumber;
	}, this);
	rO.totalTimes = rO.failureFrequency + rO.passedFrequency;

	return taskList;
};
Report.prototype.getResultList = function () {
	return this.$resultList;
};
Report.prototype.getTask = function () {
	return this.$task;
};
Report.prototype.getOverview = function () {
	return this.$overview;
};

Report.extendFn = function (name, fn, isOverride) {
	if (!this.prototype.hasOwnProperty(name) || isOverride) {
		this.prototype[name] = fn;
	} else {
		throw new Error('Method named ' + name + ' has been existed.');
	}
	return this;
};

module.exports = Report;
